import {
  RecordUserId,
  getUserVip
} from '@/api/user.js'

import {
  storePethealthItem
} from '@/api/home.js'

import {
  removechives,
  newShopchives
} from '@/api/new_pet.js'

export default {
  // 为当前模块开启命名空间
  namespaced: true,

  // 模块的 state 数据
  state: () => ({
    // 我的宠物
    getRecordItem: [],
    // 宠物详情
    RecordCategoryList: {},
    // 宠物id
    RecordCategoryId: 0,
    // 宠物vip
    getUserVipItem: [],
  }),

  // 模块的 mutations 方法
  mutations: {
    getStoresRecord(state, getRecordItem) {
      state.getRecordItem = getRecordItem
    },
    getCategoryList(state, CategoryList) {
      state.RecordCategoryList = CategoryList
      state.RecordCategoryList.labels = CategoryList.labels.split(',')
    },
    getRecordCategoryId(state, RecordCategoryId) {
      state.RecordCategoryId = RecordCategoryId
    },
    getRecordCategoryVip(state, getUserVipItem) {
      state.getUserVipItem = getUserVipItem
    },
  },

  // 模块的 getters 属性
  getters: {
    getRecordItem: state => state.getRecordItem,
    RecordCategoryList: state => state.RecordCategoryList,
    RecordCategoryId: state => state.RecordCategoryId,
    getUserVipItem: state => state.getUserVipItem,
  },
  actions: {
    // 获取列表
    GetRecordItem({
      commit
    }) {
      RecordUserId().then(res => {
        // console.log(res.data)
        commit('getStoresRecord', res.data)
      })
    },
    // 获取宠物详情
    GetCategoryList({
      commit
    }, id) {
      storePethealthItem(id).then(res => {
        commit('getCategoryList', res.data)
      })
    },
    // 删除宠物
    RemoveCategoryList({
      commit,
      dispatch
    }, id) {
      removechives(id).then(res => {
        // console.log(res.data)
        dispatch('GetRecordItem')
      })
    },
    // 添加
    AddCategoryList({
      commit,
      dispatch,
      state
    }, user) {
      // console.log(user)
      newShopchives(user).then(res => {
        if (res.msg === '内容含有违法违规内容') {
          return uni.showToast({
            title: '内容含有违法违规内容 ~',
            icon: 'none'
          })
        }
        // console.log(res)
        commit('getRecordCategoryId', res.data.id)
        uni.redirectTo({
          url: '/subpkg/recordyes/recordyes?id=' + state.RecordCategoryId
        })
        dispatch('GetRecordItem')
      })
    },
    // 宠物会员卡
    GetUserVipItem({
      commit,
      dispatch
    }) {
      getUserVip().then(res => {
        // console.log(res.data)
        if (res.data) {
          commit('getRecordCategoryVip', res.data[0])
        }
      })
    },
  }
}
